﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SchoolService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ServiceSchool" in code, svc and config file together.
    public class ServiceSchool : IServiceSchool
    {
        public string AddClass(string className)
        {
            try
            {
                SchoolEntities context = new SchoolEntities();
                context.Classes.AddObject(new Class() { Name = className });
                context.SaveChanges();
                return string.Format("Class {0} added", className);
            }
            catch (Exception e)
            {
                return string.Format("Error occured while adding the class\"{0}\". Exception:{1}", className, e.Message);
            }
        }


        public string DeleteClass(string className)
        {
            try
            {
                SchoolEntities context = new SchoolEntities();
                Class toDelete = context.Classes.FirstOrDefault(c => c.Name == className);
                if (toDelete == null)
                {
                    throw new KeyNotFoundException();
                }
                context.Classes.DeleteObject(toDelete);
                context.SaveChanges();
                return string.Format("Class {0} deleted", className);
            }
            catch (KeyNotFoundException)
            {
                return string.Format("The class \"{0}\" you are trying to delete does not exists", className);
            }
            catch (Exception e)
            {
                return string.Format("Error occured while deleting the class \"{0}\". Exception:{1}", className, e.Message);
            }
        }

        public string AddStudent(string studentName, string className)
        {
            try
            {
                SchoolEntities context = new SchoolEntities();
                Class studentClass = context.Classes.FirstOrDefault(c => c.Name == className);
                if (studentClass == null)
                {
                    throw new KeyNotFoundException();
                }
                context.Students.AddObject(new Student() { Name = studentName, ClassID= studentClass.ClassID });
                context.SaveChanges();
                return string.Format("Student {0} added.", studentName);
            }
            catch (KeyNotFoundException)
            {
                return string.Format("The class \"{0}\" does not exists.", className);
            }
            catch (Exception e)
            {
                return string.Format("Error occured while adding the student\"{0}\". Exception:{1}", className, e.Message);
            }
        }

        public string DeleteStudent(string studentID)
        {
            try
            {
                SchoolEntities context = new SchoolEntities();
                int studentIDInt = int.Parse(studentID);
                Student student = context.Students.FirstOrDefault(s=>s.StudentID == studentIDInt);
                if (student == null)
                {
                    throw new KeyNotFoundException();
                }
                context.Students.DeleteObject(student);
                context.SaveChanges();
                return string.Format("Student with ID:{0} deleted", studentID);
            }
            catch (KeyNotFoundException)
            {
                return string.Format("The student with ID:{0} does not exists", studentID);
            }
            catch (Exception e)
            {
                return string.Format("Error occured while deleting the student with ID:{0}.<br/> Exception:{1}", studentID, e.Message);
            }
        }

        public string EditStudent(string studentID, string newStudentName)
        {
            throw new NotImplementedException();
        }

        public string EditStudentClass(string studentID, string studentClass)
        {
            throw new NotImplementedException();
        }

        public string GetAllStudentsFromClass(string className)
        {
            throw new NotImplementedException();
        }

        public string AddSubject(string subjectName)
        {
            throw new NotImplementedException();
        }

        public string AddDeleteSubject(string subjectName)
        {
            throw new NotImplementedException();
        }

        public string EditSubject(string subjectName, string newSubjectName)
        {
            throw new NotImplementedException();
        }

        public string AddSubjectToClass(string subjectName, string className)
        {
            throw new NotImplementedException();
        }

        public string RemoveSubjectFromClass(string subjectName, string className)
        {
            throw new NotImplementedException();
        }

        public string GetClassSubjects(string className)
        {
            throw new NotImplementedException();
        }
    }
}
